package dataStructure.Stack;

import java.util.Scanner;

/**
 * @author lhn
 * @date 2024-04-20 13:19
 */

public class ArrayStackDemo {
    public static void main(String[] args) {
        ArrayStack stack = new ArrayStack(4);
//        stack.pop();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        stack.pop();
        stack.list();

//        String key = "";
//        boolean loop = true;
//        Scanner sc = new Scanner(System.in);
//        while(loop) {
//            System.out.println("show:显示栈");
//            System.out.println("exit:退出栈");
//            System.out.println("push:压入栈");
//            System.out.println("pop :  出栈");
//            key = sc.next();
//            switch (key) {
//                case "show":
//                    stack.list();
//                    break;
//                case "exit":
//                    sc.close();
//                    loop = false;
//                    break;
//                case "push":
//                    System.out.println("请输入一个数：");
//                    int value = sc.nextInt();
//                    stack.push(value);
//                    break;
//                case "pop":
//                    try {
//                        int res = stack.pop();
//                        System.out.printf("出栈的数据是%d\n",res);
//                    } catch (Exception e){
//                        System.out.println(e.getMessage());
//                    }
//                    break;
//            }
//        }
//        System.out.println("程序退出~~~");
    }
}
class ArrayStack{
    private int maxSize;    //栈的大小
    private int[] stack;    //数组模拟栈
    private int top = -1;   //top表示栈顶

    public ArrayStack(int maxSize) {
        this.maxSize = maxSize;
        this.stack = new int[this.maxSize];
    }

    public boolean isFull() {
        return this.top == this.maxSize - 1;
    }
    public boolean isEmpty() {
        return this.top == -1;
    }
    public void push (int value) {
        if (isFull()) {
            System.out.println("栈满");
            return;
        }
        top++;
        stack[top] = value;
    }
    public int pop() {
        if (isEmpty()) {
            System.out.println("栈空");
            throw new RuntimeException("栈为空");
        }
        int value = this.stack[top];
        top--;
        return value;
    }
    public int peek() {
        return this.stack[top];
    }
    public void list() {
        if (isEmpty()) {
            System.out.println("栈空");
            return;
        }
        int index = top;
        for (int i = top; i >= 0 ; i--) {
            System.out.printf("stack[%d]=%d\n",i,this.stack[i]);
        }
    }
}